//
//  WBPresentView.swift
//  XLWibo-Swift
//
//  Created by 王林 on 2016/11/20.
//  Copyright © 2016年 木木. All rights reserved.
//




import UIKit
import pop




let btnMargin =  (USC_width - 3 * btnW) / 4
class WBPresentView: UIView {

   lazy var btnArr:[WBAnimationBtn] = [WBAnimationBtn]()
    
  lazy var tooBar = UIToolbar(frame: UIScreen.main.bounds)
    
    lazy var imgView = UIImageView.init(image: #imageLiteral(resourceName: "compose_slogan"))
    
    override init(frame: CGRect) {
        super.init(frame: UIScreen.main.bounds)
        setupUI()
        
    }
    
    //展示视图
    func show(fromVC:UIViewController?){
        fromVC?.view.addSubview(self)
        //开始动画
        for (index,btn) in btnArr.enumerated() {
            self.startAnimation(btn: btn, index: index)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func   setupUI(){
        
        tooBar.barStyle = .`default`
        self.addSubview(backView)
        self.addSubview(tooBar)
        self.addSubview(imgView)
        
        imgView.snp.makeConstraints { (make) in
           make.centerX.equalTo(self)
            make.top.equalTo(self).offset(100)
        }
        self.createBtn()
    }
    
    private lazy var backView : UIImageView = UIImageView.init(image: UIImage.snapShopSitImage())
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        
        for (index,btn) in btnArr.reversed().enumerated() {
            startAnimation(btn: btn, index: index, isShow: false)
            
        }
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5, execute: {
            self.removeFromSuperview()
            
        })
    }
    
    //创建6个按钮 并加入数组中
    func createBtn(){

        for i in 0..<6 {
            let btn = WBAnimationBtn()
            btn.tag = i+1
             let col = 3
            let colIndex = i % col
            let rowIndex = i / col
            btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_friend"), for: .normal)
            btn.setTitle("点我啊", for: .normal)
            btn.setTitleColor(#colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1), for: .normal)
            btn.addTarget(self, action: #selector(clickBtnAnimation(btn:)), for: .touchUpInside)
            
            let X = CGFloat(colIndex)*(btnW+btnMargin)+btnMargin
            let Y = CGFloat(rowIndex)*btnH + USC_hight
            
            btn.frame = CGRect(x: X, y: Y, width: btnW, height: btnW)
            self.addSubview(btn)
            
            btnArr.append(btn)
            
        }

}
    //MARK:  -
    //MARK:  - 开始  结束 动画
    
    func startAnimation(btn:WBAnimationBtn,index:Int,isShow:Bool = true){
        
        //添加动画对象
        let springAnimation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
        
        springAnimation?.springBounciness = 10.0
        springAnimation?.springSpeed = 10
        //延迟时间
        springAnimation?.beginTime = CACurrentMediaTime() + Double(index)*0.05
        let  btnCenterY = btn.center.y + CGFloat(isShow ? -350 : 350)
        
        springAnimation?.toValue = NSValue.init(cgPoint: CGPoint(x: btn.center.x, y: btnCenterY))
        btn.pop_add(springAnimation, forKey: nil)
        
    }
    
    //MARK:  -
    //MARK:  - //点击 某一个按钮  按钮实现动画
    func clickBtnAnimation(btn:WBAnimationBtn){
        print("\(btn.tag)")
       
        UIView.animate(withDuration: 1, animations: {
            
            for Btn in self.btnArr {
                if  btn == Btn{
                    UIView.animate(withDuration: 0.25, animations: {
                        Btn.transform = CGAffineTransform.init(scaleX: 2.0, y: 2.0)
                    })
                }else{
                    UIView.animate(withDuration: 0.25, animations: {
                        Btn.transform = CGAffineTransform.init(scaleX: 0.3, y: 0.3)
                        Btn.alpha = 0.1
                    })
                }
                
            }
            
        }) { (_) in
            print("跳转")
            //根据查找
            let  bundleName = Bundle.main.infoDictionary!["CFBundleName"] as! String
            let className = bundleName + "." + "WBAnimationViewController"
            
            guard let classType = NSClassFromString(className) as? UIViewController.Type else{
                return
            }

            let AnimationVC = classType.init()
            let navi = UINavigationController.init(rootViewController: AnimationVC)
            UIApplication.shared.keyWindow?.rootViewController?.present(navi, animated: true, completion: {
                self.removeFromSuperview()
            })
            
            
            
        }
        
    }
    
    
   
    
    




}



